//
//  LCWToolBar.swift
//  MyWB
//
//  Created by 李成万 on 16/7/3.
//  Copyright © 2016年 李成万. All rights reserved.
//

import UIKit
enum LCWToolBarType :Int {
    case TuPian = 1
    case AiTe = 2
    case JingHao = 3
    case BiaoQing = 4
    case JiaHao = 5
}


class LCWToolBar: UIView {

    var toolBarClosure:((type:LCWToolBarType)->())?
    
    var btnBiaoQing : UIButton?
    
    var isKeyBoard : Bool = false {
        didSet{
            if isKeyBoard{
                btnBiaoQing?.setImage(UIImage(named:"compose_emoticonbutton_background" ), forState: UIControlState.Normal)
                btnBiaoQing?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }else{
                btnBiaoQing?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
                btnBiaoQing?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func setupUI(){
        let btnTuPian = setupBtn("compose_toolbar_picture", type: .TuPian)
        let btnAiTe = setupBtn("compose_mentionbutton_background", type: .AiTe)
        let btnJingHao = setupBtn("compose_trendbutton_background", type: .JingHao)
        btnBiaoQing = setupBtn("compose_emoticonbutton_background", type: .BiaoQing)
        let btnJiaHao = setupBtn("compose_add_background", type: .JiaHao)
        addSubview(btnTuPian)
        addSubview(btnAiTe)
        addSubview(btnJingHao)
        addSubview(btnBiaoQing!)
        addSubview(btnJiaHao)
        
        btnTuPian.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(btnAiTe)
        }
        btnAiTe.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(btnTuPian.snp_trailing)
            make.width.equalTo(btnJingHao)
        }
        btnJingHao.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(btnAiTe.snp_trailing)
            make.width.equalTo(btnBiaoQing!)
        }
        btnBiaoQing!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(btnJingHao.snp_trailing)
            make.width.equalTo(btnJiaHao)
        }
        btnJiaHao.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(btnBiaoQing!.snp_trailing)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(SCREENW)
            make.height.equalTo(44)
        }
    }
    
    /**
     创建按钮的方法
     
     - parameter imageName: 图片
     - parameter type:       tag 值
     
     - returns: 返回一个按钮
     */
    func setupBtn(imageName:String,type:LCWToolBarType)->UIButton{
        let btn = UIButton(imageName: imageName, backgroundImageName: "")
        btn.tag = type.rawValue
        btn.addTarget(self, action: "toolBarBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }
    
    func toolBarBtnClick(btn : UIButton){
        toolBarClosure?(type: LCWToolBarType(rawValue: btn.tag)!)
    }
}
